let API = {

    getFirstMenu: function (callback) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.GENERAL_IPS[0].getMenus1_new,
            data: {
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                callback(data);
            },
            dataType: 'json'
        })
    },
    getSecondMenu: function (pmenuid, pageIndex, pageSize, callback) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.GENERAL_IPS[0].getMenuByPid_new,
            data: {
                pmenuid: pmenuid,
                pageIndex: pageIndex || 0,
                pageSize: pageSize || 10,
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                callback(data);
            },
            dataType: 'json'
        })
    },

    //登陆接口
    phoneLogin: function (phoneNumber, password, userType, callback) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.GENERAL_IPS[0].phoneLogin,
            data: {
                businessId: CONFIG['businessId'],
                businessCode: CONFIG['businessCode'],
                platformCode: CONFIG['platformCode_mobile_web'],
                versionCode: CONFIG['API_VERSION'],
                phoneNumber: phoneNumber,
                password: password,
                userType: userType
            },
            success: function (data) {
                console.log(data);
                callback(data);
            },
            dataType: 'json'
        })
    },

    // 发送手机验证码
    sendMessage: function (phoneNum, resolve, reject) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.GENERAL_IPS[0].xHanNews_sendMessage_v1_0,
            data: {
                phoneNumber: phoneNum
            },
            dataType: 'json',
            success: function (res) {
                console.log(res);
                if (res.status) {
                    resolve && resolve(res);
                } else {
                    reject && reject(res.message);
                }
            },
            error: function () {
                reject && reject('服务器出错，请稍后再试');
            }
        });
    },

    // 短信验证
    validateCode: function (phoneNum, code, resolve, reject) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.GENERAL_IPS[0].xHanNews_validateCode_v1_0,
            data: {
                phoneNumber: phoneNum,
                code: code
            },
            dataType: 'json',
            success: function (res) {
                console.log('>>>>>' + res);
                if (res.status == '1') {
                    resolve && resolve(res);
                } else {
                    reject && reject(res.message);
                }
            },
            error: function () {
                reject && reject('服务器出错，请稍后再试');
            }
        });
    },

    // 找回密码
    findPwd: function (phoneNum, newPwd, uid, resolve, reject) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.GENERAL_IPS[0].phoneFindePwd,
            data: {
                phoneNumber: phoneNum,
                password: newPwd,
                ubusinessId: uid
            },
            dataType: 'json',
            success: function (res) {
                console.log(res);
                if (res.status) {
                    resolve && resolve(res);
                } else {
                    reject && reject(res.message);
                }
            },
            error: function () {
                reject && reject('服务器出错，请稍后再试');
            }
        });
    },
    //获取用户信息
    getUserInfo: function (callback) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0].userFarmersInfoV10,
            data: {
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                callback(data);
            },
            dataType: 'json'
        })
    },
    updateUserInfo: function (userId, sex, userealinfoId, headPortraitFilepath, success, defeat, error) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0].userAddUpdateV10 + '&userId=' + userId + '&sex=' + sex + '&judge=picture' + '&userealinfoId=' + userealinfoId + '&loginToken=' + LOGIN_TOKEN,
            type: 'POST',
            data: {
                headPortraitFilepath: headPortraitFilepath,
            },
            success: function (data) {
                console.log(data);
                if (data.status === 1) {
                    success && success(data)
                } else {
                    defeat && defeat(data)
                }
            },
            error: function () {
                error && error();
            },
            dataType: 'json'
        })
    },
    // 获取banner置顶
    getTopNews: function (menuID, callback) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0].getLocalLawTopList,
            data: {
                menuId: menuID,
                pageIndex: 0,
                pageSize: 5,
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                if (data.status) {
                    callback(data);
                }
            },
            dataType: 'json'
        })
    },
    // 获取消息通知
    getNotice: function (success, defeat, error) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0].culturaSouvenirUserPraise,
            data: {
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                if (data.status === 1) {
                    success && success(data)
                } else {
                    defeat && defeat(data)
                }
            },
            error: function () {
                error && error();
            },
            dataType: 'json'
        })
    },
    //获取列表
    getDataList: function (action,parameters, success, defeat, error) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0][action],
            data: (function () {
                let data = {
                    menuId:tools.getUrlParam('menuId'),
                    pageIndex:  0,
                    pageSize:  10,
                    loginToken: LOGIN_TOKEN
                };
                console.log($.extend(data,parameters));
                return $.extend(data,parameters)
            })(),
            success: function (data) {
                console.log(data);
                if (data.status === 1) {
                    success && success(data)
                } else {
                    defeat && defeat(data)
                }
            },
            error: function () {
                error && error();
            },
            dataType: 'json'
        })
    },

    //获取列表ByID
    getDataById: function (action, id, success, defeat, error) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0][action],
            data: {
                objId: id,
                id: id,
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                if (data.status === 1) {
                    success && success(data)
                } else {
                    defeat && defeat(data)
                }
            },
            error: function () {
                error && error();
            },
            dataType: 'json'
        })
    },
    //获取详情
    getDetailById: function (id, success, defeat, error) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0].artParticulars,
            data: {
                objId: id,
                id: id,
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                if (data.status === 1) {
                    success && success(data)
                } else {
                    defeat && defeat(data)
                }
            },
            error: function () {
                error && error();
            },
            dataType: 'json'
        })
    },
    getCommentList: function (objId, pageIndex, pageSize, success, defeat, error) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0].culturaSouvenirServiceListV10,
            data: {
                objId: objId,
                pageIndex: pageIndex,
                pageSize: pageSize,
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                if (data.status === 1) {
                    success && success(data)
                } else {
                    defeat && defeat(data)
                }
            },
            error: function () {
                error && error();
            },
            dataType: 'json'
        })
    },
    addComment: function (title, content, objId, success, defeat, error) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0].culturaSouvenirPraise,
            data: {
                title: title,
                content: content,
                objId: objId,
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                if (data.status === 1) {
                    success && success(data)
                } else {
                    defeat && defeat(data)
                }
            },
            error: function () {
                error && error();
            },
            dataType: 'json'
        })
    },
    addReply: function (objId, content, userId, userName, success, defeat, error) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0].culturaReplyPraise,
            data: {
                content: content,
                objId: objId,
                userId: userId,
                userName: userName,
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                if (data.status === 1) {
                    success && success(data)
                } else {
                    defeat && defeat(data)
                }
            },
            error: function () {
                error && error();
            },
            dataType: 'json'
        })
    },
    getReplyById: function (objId, success, defeat, error) {
        $.ajax({
            url: CONFIG.IP_CONFIG+CONFIG.VILLAGE_URLS[0].culturaSouvenirUserPraiseList,
            data: {
                objId: objId,
                loginToken: LOGIN_TOKEN
            },
            success: function (data) {
                console.log(data);
                if (data.status === 1) {
                    success && success(data)
                } else {
                    defeat && defeat(data)
                }
            },
            error: function () {
                error && error();
            },
            dataType: 'json'
        })
    }
};

let tools = (function () {
    let obj = {
        getStore: function (name, toParse) {
            if (!name) return;
            try {
                let result = window.localStorage.getItem(name);
                result = toParse ? JSON.parse(result) : result;
                return result ? result : null;
            } catch (err) {
                console.log('此设备不支持localStorage!');
            }
        },

        setStore: function (name, value) {
            if (!name) return;
            try {
                value = value === undefined || typeof value === 'function' ? value + '' : JSON.stringify(value);
                window.localStorage.setItem(name, value);
                return true;
            } catch (err) {
                console.log('此设备不支持localStorage!');
                return false;
            }
        },

        removeStore: function (name) {
            if (!name) return;
            try {
                window.localStorage.removeItem(name);
                return true;
            } catch (err) {
                console.log('此设备不支持localStorage!');
                return false;
            }
        },
        // 获取url参数
        getUrlParam: function (name) {
            let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
            let result = window.location.search.substring(1).match(reg);
            return result ? decodeURIComponent(result[2]) : null;
        },
        //跳转菜单
        goSkip: function () {
            $('body').on('click', '[data-url]', function () {
                location.href = $(this).data('url');
            })
        },
        layerMsg: function (content) {
            layer.open({
                skin: 'msg',
                content: content,
                time: 2
            })
        },
        HTMLEncode: function (html) {
            let temp = document.createElement('div');
            (temp.textContent !== null) ? (temp.textContent = html) : (temp.innerText = html);
            let output = temp.innerHTML;
            temp = null;
            return output;
        },
        HTMLDecode: function (text) {
            let temp = document.createElement('div');
            temp.innerHTML = text;
            let output = temp.innerText || temp.textContent;
            temp = null;
            return output;
        }
    };


    return obj;
})();

Date.prototype.Format = function (fmt) { //author: meizz
    let o = {
        'M+': this.getMonth() + 1, //月份
        'd+': this.getDate(), //日
        'h+': this.getHours(), //小时
        'm+': this.getMinutes(), //分
        's+': this.getSeconds(), //秒
        'q+': Math.floor((this.getMonth() + 3) / 3), //季度
        'S': this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    for (let k in o)
        if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
    return fmt;
};


let SERVER = '';
let BASE_URL = '';
let CONFIG_NAME = '';
let LOGIN_TOKEN_NAME = '';
let LOGIN_DATA_NAME = '';
let TEST_TOKEN = '';
$.ajaxSettings.async = false;
$.getJSON('../config.json', function (data) {
    SERVER = data.server;
    BASE_URL = data.baseUrl;
    CONFIG_NAME = data.configName;
    LOGIN_TOKEN_NAME = data.loginTokenName;
    LOGIN_DATA_NAME = data.loginDataName;
    TEST_TOKEN = data.testToken;
});
$.ajaxSettings.async = true;

if (!tools.getStore(CONFIG_NAME, true)) {
    $.ajaxSettings.async = false;
    $.getJSON(SERVER + BASE_URL, function (data) {
        tools.setStore(CONFIG_NAME, data);
    });
    $.ajaxSettings.async = true;
}

let CONFIG = tools.getStore(CONFIG_NAME, true);
let LOGIN_TOKEN = tools.getStore(LOGIN_TOKEN_NAME, true) || TEST_TOKEN;
let LOGIN_DATA = tools.getStore(LOGIN_DATA_NAME, true);
